package com.nbb.wechat.wechatpcrobot;

import cn.hutool.core.date.DateUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * @description:
 * @author: Jerry
 * @date: 2021/8/17
 */
public class RobotFrame {

    private JFrame winFrame = new JFrame("winFrame");

    private int bounder = 10;

    private JLabel sendTimeLable = new JLabel("发送时间");
    private JLabel sendContentLable = new JLabel("发送内容");
    private JLabel toLable = new JLabel("接收人名称");
    private JLabel commitTimeLable = new JLabel();
    private JLabel nowTimeLable = new JLabel();

    private JTextField timeFiled = new JTextField(14);
    private JTextField toFiled = new JTextField(14);
    private JTextArea sendTextArea = new JTextArea(5, 14);

    private Font btnFont = new Font("Default", 0, 20);
    private Font txtFont = new Font("Default", 0, 40);

    private JButton btnCommit = new JButton("提交");

    private WechatSendRobot wechatSendRobot = new WechatSendRobot();

    private Date sendTime;



    private Font getFont(int size) {
        return new Font("Default", 0, size);
    }

    private void init() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int width = toolkit.getScreenSize().width;
        int height = toolkit.getScreenSize().height;
        int frameWidth = 500;
        int frameHeight = 650;
        winFrame.setBounds((width - frameWidth) / 2, (height - frameHeight) / 2, frameWidth, frameHeight);
//        winFrame.setLayout(null);
        winFrame.setTitle("微信定时发送消息");
        winFrame.setSize(frameWidth, frameHeight);
//        winFrame.setLocation(400, 200);
        winFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        winFrame.setResizable(false);
        JPanel centerPanel = new JPanel(new GridLayout(0, 1, 25, 25));
        JPanel centerPanelFlow = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
//        centerPanel.setOpaque(false);
        centerPanelFlow.add(sendTimeLable);
        centerPanelFlow.add(timeFiled);
        centerPanelFlow.add(toLable);
        centerPanelFlow.add(toFiled);
        centerPanelFlow.add(sendContentLable);
        centerPanelFlow.add(new JScrollPane(sendTextArea));
        centerPanelFlow.add(nowTimeLable);
        centerPanelFlow.add(commitTimeLable);
        centerPanel.add(centerPanelFlow);
        JPanel southPanel = new JPanel(new GridLayout(1, 1, 5, 5));
        JPanel southPanelFlow = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        southPanelFlow.add(btnCommit);
        southPanel.add(southPanelFlow);
        JPanel borderPanel = new JPanel(new BorderLayout());
//        borderPanel.setOpaque(false);
//        borderPanel.add(northPanel, BorderLayout.NORTH);
        borderPanel.add(centerPanel, BorderLayout.CENTER);
        borderPanel.add(southPanel, BorderLayout.SOUTH);
        winFrame.add(borderPanel);
        timeFiled.setText(DateUtil.now());
        sendTextArea.setFont(txtFont);
        sendContentLable.setFont(getFont(20));
        toLable.setFont(getFont(20));
        commitTimeLable.setFont(getFont(20));
        nowTimeLable.setFont(getFont(20));
        sendTimeLable.setFont(getFont(20));
//        private JLabel toLable = new JLabel("接收人名称");
//        private JLabel commitTimeLable = new JLabel();
//        private JLabel nowTimeLable = new JLabel();
        sendTextArea.setAutoscrolls(true);
        sendTextArea.setLineWrap(true);
        sendTextArea.setWrapStyleWord(true);
        btnCommit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点击提交");
                String btnCommitText = btnCommit.getText();
                if (btnCommitText.equals("提交")) {
                    btnCommit.setText("取消");
                    timeFiled.setEditable(false);
                    toFiled.setEditable(false);
                    sendTextArea.setEditable(false);
                    commitTimeLable.setText("提交时间：" + DateUtil.now());
                    sendTime = DateUtil.parseDateTime(timeFiled.getText().trim());
                } else if (btnCommitText.equals("取消")) {
                    timeFiled.setEditable(true);
                    toFiled.setEditable(true);
                    sendTextArea.setEditable(true);
                    btnCommit.setText("提交");
                    commitTimeLable.setText(null);
                }
            }
        });
        timeFiled.setFont(txtFont);
        toFiled.setFont(txtFont);
        btnCommit.setFont(btnFont);
//        winFrame.pack();
//        ImageIcon image = new ImageIcon("icon.png");
//        winFrame.setIconImage(image.getImage());
        Image image1 = toolkit.getImage("icon.png");
        winFrame.setIconImage(image1);

        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nowTimeLable.setText("当前时间：" + DateUtil.now());
                String btnCommitText = btnCommit.getText();
                if (btnCommitText.equals("取消") && System.currentTimeMillis() - sendTime.getTime() > 0) {
                    wechatSendRobot.send(toFiled.getText(), sendTextArea.getText());
                    timeFiled.setEditable(true);
                    toFiled.setEditable(true);
                    sendTextArea.setEditable(true);
                    btnCommit.setText("提交");
                    commitTimeLable.setText(null);
                }
            }
        });
        timer.start();
    }

    public void show() {
        init();
        winFrame.setVisible(true);
    }

    public static void main(String[] args) {
        RobotFrame robotFrame = new RobotFrame();
        robotFrame.show();
    }
}
